package com.log4jviewer.logfile.fields;

/**
 * Class constructs Regex for log's level field.
 * 
 * @author <a href="mailto:rd.ryly@gmail.com">Ruslan Diachenko</a>
 */
public class LevelField extends AbstractField {

    public LevelField(LogFieldName logFieldName, boolean leftAlign, boolean rightAlign) {
        super(logFieldName, leftAlign, rightAlign);
    }

    @Override
    public LogLevelName decodeValue(String value) {
        return Enum.valueOf(LogLevelName.class, value);
    }

    @Override
    public String getRawRegex() {
        StringBuilder rawRegex = new StringBuilder();
        rawRegex.append("(?:");

        for (LogLevelName logLevelName : LogLevelName.values()) {
            rawRegex.append(logLevelName).append('|');
        }

        // Replace the last character '|' to ')'.
        int start = rawRegex.length() - 1;
        int end = rawRegex.length();
        rawRegex.replace(start, end, ")");

        return rawRegex.toString();
    }
}
